package com.example.demo.proxy;



import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2024-02-23
 * Time: 11:42
 */
public class Main {
    public static void main(String[] args) {
//        HouseSubject houseSubject =new RealHouseSubject();
//
//        HouseProxy proxy = new HouseProxy(houseSubject);
//
//        //通过代理类访问目标方法
//        proxy.rentHouse();
//
//        //通过代理类访问目标方法
//        proxy.saleHouse();

        //代理接口
        HouseSubject target = new RealHouseSubject();
//        HouseSubject proxy = (HouseSubject) Proxy.newProxyInstance(target.getClass().getClassLoader(),
//                new Class[]{HouseSubject.class},
//                new JDKInvocation(target));
//        proxy.rentHouse();

        //代理类
        HouseSubjectClass houseSubjectClass = new HouseSubjectClass();
//        HouseSubjectClass proxy1 = (HouseSubjectClass) Proxy.newProxyInstance(houseSubjectClass.getClass().getClassLoader(),
//                new Class[]{HouseSubjectClass.class},
//                new JDKInvocation(houseSubjectClass));
//        proxy1.rentHouse();


        //代理接口
        HouseSubject proxy2 = (HouseSubject) Enhancer.create(target.getClass(),new CGLIBInterceptor(target));
        proxy2.rentHouse();

        //代理类
        HouseSubject proxy4 = (HouseSubject) Enhancer.create(houseSubjectClass.getClass(),new CGLIBInterceptor(houseSubjectClass));
        proxy4.rentHouse();

    }
}
